//
//  UIImage+ImageCompose.m
//  CoolMove
//
//  Created by CA on 15/8/27.
//  Copyright (c) 2015年 CA. All rights reserved.
//

#import "UIImage+ImageCompose.h"
#import <Accelerate/Accelerate.h>

@implementation UIImage (ImageCompose)

- (UIImage *)composeImageSize:(CGSize)finalSize centerImage:(UIImage *)image cnterImageSize:(CGSize)centerSize
{
    UIGraphicsBeginImageContext(finalSize);
    [self drawInRect:CGRectMake(0,0,finalSize.width,finalSize.height)];
    CGFloat minLength = MIN(finalSize.height, finalSize.width);
    
    CGFloat finalLength = MIN(minLength/2.0f, centerSize.height);
    
    [image drawInRect:CGRectMake(finalSize.width/2.0f - finalLength/2.0f, finalSize.height/2.0f - finalLength/2.0f, finalLength, finalLength)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

- (UIImage *)creatGaussianblur {
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:self.CGImage];
    // create gaussian blur filter
    
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:inputImage forKey:kCIInputImageKey];
    [filter setValue:[NSNumber numberWithFloat:20.0] forKey:@"inputRadius"];
    // blur image
    CIImage *result = [filter valueForKey:kCIOutputImageKey];
    CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    return image;
}

+ (void)creatBlurBackgroundWith:(UIImage *)image view:(UIView *)view blurRadius:(CGFloat)blurRadius {
    CIImage *originImage = [CIImage imageWithCGImage:image.CGImage];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:originImage forKey:kCIInputImageKey];
    [filter setValue:[NSNumber numberWithFloat:blurRadius] forKey:@"inputRadius"];
    // 生成模糊图片
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *result = [filter valueForKey:kCIOutputImageKey];
    UIImage *blurImage = [UIImage imageWithCGImage:[context createCGImage:result fromRect:result.extent]];
    // 将模糊图片加入背景
    CGFloat width = view.frame.size.width;
    CGFloat height = view.frame.size.height;
    UIImageView *blurImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-1.5 * blurRadius, -1.5 *blurRadius, width + blurRadius * 3,height + blurRadius * 3)];
    blurImageView.contentMode = UIViewContentModeScaleAspectFill;
    blurImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    blurImageView.image = blurImage;
    [view insertSubview:blurImageView atIndex:0];
    
}


+(UIImage *)boxblurImage:(UIImage *)image withBlurNumber:(CGFloat)blur {
    if (blur < 0.f || blur > 1.f) {
        blur = 0.5f;
    }
    int boxSize = (int)(blur * 40);
    boxSize = boxSize - (boxSize % 2) + 1;
    
    CGImageRef img = image.CGImage;
    
    vImage_Buffer inBuffer, outBuffer;
    vImage_Error error;
    
    void *pixelBuffer;
    //从CGImage中获取数据
    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
    //设置从CGImage获取对象的属性
    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);
    
    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
    
    pixelBuffer = malloc(CGImageGetBytesPerRow(img) *
                         CGImageGetHeight(img));
    
    if(pixelBuffer == NULL)
        NSLog(@"No pixelbuffer");
    
    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(img);
    outBuffer.height = CGImageGetHeight(img);
    outBuffer.rowBytes = CGImageGetBytesPerRow(img);
    
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    
    if (error) {
        NSLog(@"error from convolution %ld", error);
    }
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(
                                             outBuffer.data,
                                             outBuffer.width,
                                             outBuffer.height,
                                             8,
                                             outBuffer.rowBytes,
                                             colorSpace,
                                             kCGImageAlphaNoneSkipLast);
    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
    
    //clean up
    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);
    
    free(pixelBuffer);
    CFRelease(inBitmapData);
    
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageRef);
    
    return returnImage;
}
@end
